package org.odk.clinic.android.activities;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.text.DateFormat;

import org.odk.clinic.android.R;
import org.odk.clinic.android.database.SQLiteAdapter;
import org.odk.clinic.android.openmrs1.patients;
import org.odk.clinic.android.openmrs1.person;
import org.odk.clinic.android.openmrs1.person_address;
import org.odk.clinic.android.openmrs1.person_name;
import org.odk.clinic.android.openmrs1.user;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;

public class NewPatientActivity1 extends Activity{
	//
	private DateFormat mDateFormat = new SimpleDateFormat("dd-mm-yyyy");
	private SQLiteAdapter mDB;
	private Cursor mcursor;
	//
	private EditText mDateDisplay,mgivenname,mmidlename,mfamilyname,maddress,memail,mphone;
	private RadioGroup mgender;
	private Button mPickDate,mNext,mCancel;
	private Spinner spinnerbirthplace;
	private int mYear;
	private int mMonth;
	private int mDay;
	static final int DATE_DIALOG_ID = 0;
	//
	private person p;
	private person_name pna;
	private person_address padd;
	private patients pa;
	private user us;
	//
	protected void onCreate(Bundle ici){
		super.onCreate(ici);
		setContentView(R.layout.add_patient_1);
		setTitle(getString(R.string.app_name) + ">"
				+ getString(R.string.add_patient1));
		//
		spinnerbirthplace =(Spinner)this.findViewById(R.id.spinbirthplace);
		ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.city, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerbirthplace.setAdapter(adapter);
		//
		mDateDisplay = (EditText)this.findViewById(R.id.birthday);
		mPickDate = (Button)this.findViewById(R.id.btnbirth);
		mPickDate.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDialog(DATE_DIALOG_ID);
			}
		});
		//get current date
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		
		//display the current date
		updateDisplay();
		//Button next
		 //Nếu 1 trong 3 Edit Text không có nội dung thì hiện lên thông báo
       /* if (workEnter.getText().toString().equals("") ||
                hourEdit.getText().toString().equals("") ||
                minuteEdit.getText().toString().equals("")) {                    
            AlertDialog.Builder builder = new AlertDialog.Builder(Example.this);
            builder.setTitle("Info missing");
            builder.setMessage("Please enter all information of the work");
            builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub                            
                }                        
            });
            builder.show();
        }*/
		mgivenname = (EditText)this.findViewById(R.id.givenname);
		mmidlename = (EditText)this.findViewById(R.id.midlename);
		mfamilyname = (EditText)this.findViewById(R.id.familyname);
		maddress = (EditText)this.findViewById(R.id.address);
		mgender = (RadioGroup)this.findViewById(R.id.gender);
		memail = (EditText)this.findViewById(R.id.email);
		mphone = (EditText)this.findViewById(R.id.phone);
		
		mNext = (Button)this.findViewById(R.id.btnpatientnext);
		mNext.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mDB = new SQLiteAdapter(NewPatientActivity1.this);
				mDB.open();
				
				p = new person();pna = new person_name();padd = new person_address() ; pa = new patients();us = new user();
				String gen = null;
				switch(mgender.getCheckedRadioButtonId()){
				case R.id.gendermale:
					gen = "M";break;
				case R.id.genderfemale:
					gen = "F";break;
				}
				p.setGender(gen);
				try{
				p.setBirth(mDateFormat.parse(mDateDisplay.getText().toString()));
				}
				catch (ParseException e ){
					e.printStackTrace();
				}
				pna.setGivenName(mgivenname.getText().toString());
				pna.setMiddleName(mmidlename.getText().toString());
				pna.setFamily_Name(mfamilyname.getText().toString());
				pna.setEmail(memail.getText().toString());
				pna.setPhone(mphone.getText().toString());
				padd.setAddress(maddress.getText().toString());
				padd.setStateProvince(spinnerbirthplace.getSelectedItem().toString());
				mDB.createPatient(pna,p,pa,padd, us);
				Intent i = new Intent(NewPatientActivity1.this,ListPatientActivity2.class);
				startActivity(i);
				
			}
		});
		//button cancel
		mCancel =(Button)this.findViewById(R.id.btncancel);
		mCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				;
			}
		});
	}
	//UPDATE THE DATE
    private void updateDisplay(){
    	mDateDisplay.setText(new StringBuilder()
    	//month is o base so add 1
    	.append(mDay).append("-")
    	.append(mMonth + 1).append("-")
    	.append(mYear)
    	);
    }
    //THE CALL BACK RECEIVED WHEN THE USER "SETS" THE DATE IN THE DIALOG 
    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			// TODO Auto-generated method stub
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			updateDisplay();
		}
	};
	@Override
	protected Dialog onCreateDialog(int id){
		switch(id){
		case DATE_DIALOG_ID :
			return new DatePickerDialog(this, mDateSetListener,mYear,mMonth,mDay);
		}
		return null;
  
	}
	
}
